package BiTree;

/**
 * @program: java_concurrent
 * @description: 验证是否是二叉搜索树
 * @author: lxf
 * @create: 2024-06-27 15:46
 **/
public class IsValidBST {
    public static void main(String[] args) {
        TreeNode node3 = new TreeNode(6, new TreeNode(3), new TreeNode(7));
        TreeNode node2 = new TreeNode(4, null, null);
        TreeNode node1 = new TreeNode(5, node2, node3);

        TreeNode node6 = new TreeNode(3, null, null);
        TreeNode node5 = new TreeNode(1, null, null);
        TreeNode node4 = new TreeNode(2, node5, node6);
        System.out.println(isValidBST(node1));
    }
    private static long pre = Long.MIN_VALUE;
    public static boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean validBST1 = isValidBST(root.left);
        if (root.val <= pre) {return false;}
        pre = root.val;
        boolean validBST = isValidBST(root.right);
        return validBST && validBST1;
    }
}
